package com.demo.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class BreathingLightView extends View {
    private float mInitialRadius;   // 初始波纹半径
    private float mMaxRadius;   // 最大波纹半径
    private float mMaxRadiusRate = 0.25f;
    private boolean mMaxRadiusSet;

    private View parentView;
    private float max =300.0f;
    private float mCurrentRadius;
    private float newRadius;
    private Paint mPaint;
    private AnimatorSet mAnimatorSet = new AnimatorSet();

    private float mMinRadius;
    private Handler mHanler ;
    private int EVENT_AUDIO = 1;

    public void setParentView(View parentView) {
        this.parentView = parentView;
    }

    public BreathingLightView(Context context) {
        super(context);
        init();
    }

    public BreathingLightView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    private void init(){

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.argb(255, 219, 219, 219));
        mCurrentRadius = mMinRadius;
        mHanler = new Handler();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mMaxRadius = Math.min(w, h) / 2;
    }
    public void setStyle(Paint.Style style) {
        mPaint.setStyle(style);
    }



    public void setColor(int color) {
        mPaint.setColor(color);
    }

    public void animateRadius(float radius){
        if(radius <= mCurrentRadius){
            return;
        }
        if(radius > mMaxRadius){
            radius = mMaxRadius;
        }else if(radius < mMinRadius){
            radius = mMinRadius;
        }
        if(radius == mCurrentRadius){
            return;
        }
        if(mAnimatorSet.isRunning()){
            mAnimatorSet.cancel();
        }
        mAnimatorSet.playSequentially(
                ObjectAnimator.ofFloat(this, "CurrentRadius", getCurrentRadius(), radius).setDuration(50),
                ObjectAnimator.ofFloat(this, "CurrentRadius", radius, mMinRadius).setDuration(600)
        );
        mAnimatorSet.start();
    }

    public float getCurrentRadius() {
        return mCurrentRadius;
    }
    public void setCurrentRadius(float currentRadius) {
        mCurrentRadius = currentRadius;
        invalidate();
    }
    protected void onDraw(Canvas canvas) {
        if (parentView==null)return;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            int w = parentView.getWidth();
            int h = parentView.getHeight();
             w = w-h;
            float dp = w*1.f/h;
            float radius = mCurrentRadius/6;
            canvas.translate(getWidth() /2-w/2- radius *dp, getHeight() /2-h/2- radius);
            canvas.drawRect(0, 0, w+ radius *2*dp, h+ radius *2, mPaint);
            canvas.translate(-h/2- radius, 0);
            canvas.drawArc(0, 0, h+ radius *2, h+ radius *2, 90, 180, true, mPaint);
            canvas.translate(w+ radius *2*dp, 0);
            canvas.drawArc(0, 0, h+ radius *2, h+ radius *2, -90, 180, true, mPaint);
        }
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility==VISIBLE){
            mHanler.post(new Runnable() {
                @Override
                public void run() {
                    Log.d("calculateVolume","Runnable");
                    animateRadius(newRadius);
                    mHanler.postDelayed(this, 50);
                }
            });
        }else {
            mHanler.removeCallbacksAndMessages(null);
        }
    }

    public void calculateVolume(short v){
        if (v < 0) {
            v = (short) -v;
        }
        newRadius = v*1.0f/max*20.0f;
        if (newRadius >350){
            newRadius = 350;
        }

        Log.d("calculateVolume","dd "+ newRadius);
    }



}
